package com.bristua.mall.user.biz;

import com.bristua.mall.common.exception.BizException;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.user.dao.TUserCashFlowMapper;
import com.bristua.mall.user.dao.TUserChargeMapper;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.enums.UserCode;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserCashFlow;
import com.bristua.mall.user.model.TUserCharge;
import com.bristua.mall.user.model.condition.TUserCashFlowExample;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/19.
 */
@Service
@Transactional
public class WxAppChargeVipAsyncCallbackBiz {

    @Resource
    private TUserMapper tUserMapper;

    @Resource
    private TUserChargeMapper tUserChargeMapper;

    @Resource
    private TUserCashFlowMapper tUserCashFlowMapper;

    @Async
    public void chargeVipAsyncCallback(Map<String,String> map){
        if(map!=null){

            String transaction_id = map.get("transaction_id");
            TUserCashFlowExample tUserCashFlowExample = new TUserCashFlowExample();
            tUserCashFlowExample.createCriteria().andTransactionIdEqualTo(transaction_id);
            List<TUserCashFlow> tOrderPayFlows = tUserCashFlowMapper.selectByExample(tUserCashFlowExample);
            if(tOrderPayFlows==null || tOrderPayFlows.size()==0){
                String out_trade_no = map.get("out_trade_no");
                TUserCharge tUserCharge = tUserChargeMapper.selectByPrimaryKey(out_trade_no);

                if(tUserCharge==null){
                    throw new BizException(UserCode.VIP_CHARGE_ORDER_IS_NOT_EXIST.getCode(),UserCode.VIP_CHARGE_ORDER_IS_NOT_EXIST.getMsg());
                }

                String total_fee = map.get("total_fee");
                if(!total_fee.equals(tUserCharge.getChargeAmount())){
                    throw new BizException(UserCode.VIP_CHARGE_ORDER_AMT_EXCEPTION.getCode(),UserCode.VIP_CHARGE_ORDER_AMT_EXCEPTION.getMsg());
                }
                TUserCharge tUserCharge1 = new TUserCharge();

                tUserCharge1.setStatus("1");
                tUserCharge1.setChargeId(tUserCharge.getChargeId());
                tUserChargeMapper.updateByPrimaryKeySelective(tUserCharge1);

                String time_end = map.get("time_end");

                TUserCashFlow tUserCashFlow = new TUserCashFlow();
                tUserCashFlow.setCreateTime(time_end);
                tUserCashFlow.setFlowCash(total_fee);
                tUserCashFlow.setFlowType("3");
                tUserCashFlow.setOrderId(tUserCharge.getChargeId());
                tUserCashFlow.setPayType("1");
                tUserCashFlow.setRemark("VIP充值,充值成功");
                tUserCashFlow.setTransactionId(transaction_id);
                tUserCashFlow.setUserId(tUserCharge.getUserId());
                TUser tUser = tUserMapper.selectByPrimaryKey(tUserCharge.getUserId());
                TUser vipUser = new TUser();
                vipUser.setUserLevel("1");
                vipUser.setUserId(tUser.getUserId());
                tUserMapper.updateByPrimaryKeySelective(vipUser);
                tUserCashFlow.setUserName(tUser.getUserName());
                tUserCashFlow.setUserCashFlowId(SequenceUtil.nextId());
                tUserCashFlowMapper.insert(tUserCashFlow);
            }

        }
    }
}
